package com.ewanghuiju.app.presenter.my;

import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.RxPresenter;
import com.ewanghuiju.app.base.contract.my.WalletCashContract;
import com.ewanghuiju.app.component.RxBus;
import com.ewanghuiju.app.model.DataManager;
import com.ewanghuiju.app.model.bean.response.RuleBean;
import com.ewanghuiju.app.model.bean.response.ZfbInfoBean;
import com.ewanghuiju.app.model.event.SendEvent;
import com.ewanghuiju.app.model.http.response.BaseResponse;
import com.ewanghuiju.app.model.http.response.Optional;
import com.ewanghuiju.app.util.RxUtil;
import com.ewanghuiju.app.widget.CommonSubscriber;

import javax.inject.Inject;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Predicate;

/**
 * @author sj  qq:751943509
 * @class 支付宝充值
 * @time 2020/2/16
 */
public class WalletCashPresenter extends RxPresenter<WalletCashContract.View> implements WalletCashContract.Presenter {

    private DataManager mDataManager;

    @Inject
    public WalletCashPresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    @Override
    public void getAlipayinfo() {
        addSubscribe(mDataManager.getAlipayinfo()
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<ZfbInfoBean>>(mView) {
                    @Override
                    public void onNext(Optional<ZfbInfoBean> optional) {
                        if (mView != null)
                            mView.showAlipayinfo(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (mView != null)
                            mView.showAlipayinfoFail();
                    }
                })
        );
    }

    @Override
    public void getPtiguize(String type) {
        //获取规则
        addSubscribe(mDataManager.getPtiguize(type)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<RuleBean>>(mView) {
                    @Override
                    public void onNext(Optional<RuleBean> optional) {
                        if (mView != null)
                            mView.showPtiguize(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

    @Override
    public void withdraw(String price, String type, String realname, String alipay, String password) {
        //提现
        addSubscribe(mDataManager.withdraw(price, type, realname, alipay, password)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannullWithToast())
                .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView) {
                    @Override
                    public void onNext(Optional<BaseResponse> optional) {
                        if (mView != null)
                            mView.withdrawSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

    @Override
    public void earningsWithdraw(String price, String type, String realname, String alipay, String password) {
        //收益提现
        addSubscribe(mDataManager.earningsWithdraw(price, type, realname, alipay, password)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannullWithToast())
                .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView) {
                    @Override
                    public void onNext(Optional<BaseResponse> optional) {
                        if (mView != null)
                            mView.withdrawSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }


    @Override
    public void attachView(WalletCashContract.View view) {
        super.attachView(view);
        registerEvent();
    }

    /**
     * 刷新支付宝信息
     */
    private void registerEvent() {
        addSubscribe(RxBus.getDefault().toFlowable(SendEvent.class)
                .compose(RxUtil.<SendEvent>rxSchedulerHelper())
                .filter(new Predicate<SendEvent>() {
                    @Override
                    public boolean test(@NonNull SendEvent sendEvent) throws Exception {
                        return sendEvent.getType() == Constants.TYPE_ALIPAY_INFO;
                    }
                })
                .subscribeWith(new CommonSubscriber<SendEvent>(mView, "") {
                    @Override
                    public void onNext(SendEvent s) {
                        getAlipayinfo();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        registerEvent();
                    }
                })
        );
    }
}
